package one.stand.controller;

import com.jmp.base.dto.SecurityLoginCrmPcPhoneRequest;
import com.jmp.base.dto.SecurityLoginCrmPcWxRequest;
import com.jmp.base.dto.SmsCaptchaRequest;
import com.jmp.feign.LoginFegin;
import com.jmp.feign.SecurityFeign;
import com.jmp.feign.SmsFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.CompanyMapperExt;
import one.stand.model.Company;
import one.stand.model.ResultModel;
import one.stand.request.SendSmsCodeRequest;
import one.stand.request.WxLoginRequest;
import one.stand.response.LoginResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Administrator
 */
@Api(description = "crm-PC基础接口")
@Slf4j
@RestController
@RequestMapping("crmPc/home")
public class HomeController extends BaseController {

    @Autowired
    private SmsFeign smsFeign;
    @Autowired
    private SecurityFeign securityFeign;
    @Autowired
    protected LoginFegin loginFegin;

    @Autowired
    private CompanyMapperExt companyMapperExt;

    @ApiOperation(value = "微信扫码接口")
    @RequestMapping("/getPcLoginImg")
    public ResultModel<String> getPcLoginImg(HttpServletRequest httpServletRequest) {
        String companyKey = httpServletRequest.getHeader("companyKey");
        log.info("请求companyKey:{}", companyKey);
        return loginFegin.getPcLoginImg(companyKey);
    }

    @PostMapping("/pcLoginCallBack")
    public ResultModel<LoginResponse> pcLoginCallBack(String code, String state) {
        SecurityLoginCrmPcWxRequest securityLoginCrmPcWxRequest = SecurityLoginCrmPcWxRequest.builder()
                .code(code)
                .state(state)
                .build();
        return securityFeign.loginCrmPcWx(securityLoginCrmPcWxRequest);
    }

    /**
     * 短信发送
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "验证码发送")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "String"),
            @ApiImplicitParam(name = "type", value = "1登录验证", required = true, paramType = "Integer"),
    })
    @PostMapping("sendSms")
    public ResultModel<Boolean> sendSms(@RequestAttribute SendSmsCodeRequest request) {
        SmsCaptchaRequest captchaRequest = SmsCaptchaRequest.builder()
                .phone(request.getPhone())
                .type(request.getType())
                .ip(request.getIp())
                .build();
        smsFeign.captchaSend(captchaRequest);
        return ResultModel.success(true);
    }

    /**
     * 短信登录
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "短信登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "companyId", value = "企业id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "smsCode", value = "验证码", required = true, paramType = "String"),
    })
    @PostMapping("crmPcLoginBySms")
    public ResultModel<LoginResponse> crmPcLoginBySms(@RequestAttribute WxLoginRequest request) {
        SecurityLoginCrmPcPhoneRequest securityLoginCrmPcPhoneRequest = SecurityLoginCrmPcPhoneRequest.builder()
                .companyId(request.getCompanyId())
                .phone(request.getPhone())
                .smsCode(request.getSmsCode())
                .build();
        return securityFeign.loginCrmPcPhone(securityLoginCrmPcPhoneRequest);
    }

    @ApiOperation(value = "根据手机号获取公司信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "Integer"),
    })
    @PostMapping("getCompanyListByPhone")
    public ResultModel<List<Company>> getCompanyListByPhone(@RequestAttribute SendSmsCodeRequest request) {
        if (StringUtils.isEmpty(request.getPhone()) || request.getPhone().length() != 11) {
            return ResultModel.success();
        }
        List<Company> list = companyMapperExt.getCompanyByUserPhone(request.getPhone());
        return ResultModel.success(list);
    }
}
